package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.framework.web.service.TokenService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 * 
 * @author zzyl_mh
 * @date 2025-08-15
 */
@Service
public class




    FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private WechatServiceImpl wechatService;
    @Autowired
    private TokenService tokenService;

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of(
            "龙腾四海",
            "虎啸山河",
            "帝王之相",
            "霸业宏图",
            "天下无双",
            "乾坤独断",
            "万界至尊",
            "战神降临",
            "傲视群雄",
            "剑指苍穹",
            "只手遮天",
            "气吞万里",
            "横扫八荒",
            "威震九州",
            "王者归来"
    );
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //1.调用微信api,获取openId
        String oppenId = wechatService.getOpenid(userLoginRequestDto.getCode());
        //2.根据openId查询用户
        FamilyMember familyMember = getOne(new QueryWrapper<FamilyMember>()
                .eq("open_id", oppenId));
        //2.1对象为空
        if(ObjUtil.isNull(familyMember)){
            familyMember = FamilyMember.builder()
                    .openId(oppenId)
                    .build();
        }

        //3.调用微信api,获取手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());

        //4.保存或者修改用户
        saveOrUpdateFamilyMember(familyMember, phone);

        //5.将用户(id,名称)存入token
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId", familyMember.getId());
        claims.put("nickName", familyMember.getName());
        String token = tokenService.createToken(claims);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(familyMember.getName());
        return loginVo;
    }

    private void saveOrUpdateFamilyMember(FamilyMember familyMember, String phone) {
        //1.判断手机号与数据库手机号是否一致
        if(!ObjUtil.equal(familyMember.getPhone(), phone)){
            //1.不一致,修改手机号
            familyMember.setPhone(phone);
        }
        //2.根据id判断新增或者修改
        if(ObjUtil.isNotEmpty(familyMember.getId())){
            //2.1存在则修改
            updateById(familyMember);
            return;
        }
        // 3.id不存在，保存新用户
        // 随机组装昵称，词组+手机号后四位
        String nickName = DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size()))
                + StringUtils.substring(familyMember.getPhone(), 7);

        familyMember.setName(nickName);
        save(familyMember);
    }
}
